<template>
	<el-form size="large" ref="loginFormRef" class="login-content-form" :model="loginForm" :rules="loginRules">
		<el-form-item class="login-animation1" prop="account">
			<el-input text placeholder="请输入账号" v-model="loginForm.account" clearable autocomplete="off">
				<template #prefix>
					<el-icon class="el-input__icon"><ele-User /></el-icon>
				</template>
			</el-input>
		</el-form-item>
		<el-form-item class="login-animation2" prop="password">
			<el-input type="password" placeholder="请输入密码" v-model="loginForm.password" autocomplete="off" show-password
				clearable>
				<template #prefix>
					<el-icon class="el-input__icon"><ele-Lock /></el-icon>
				</template>
			</el-input>
		</el-form-item>
		<el-form-item class="login-animation3" prop="code">
			<el-col :span="15">
				<el-input text maxlength="4" placeholder="请输入验证码" v-model="loginForm.code" clearable autocomplete="off">
					<template #prefix>
						<el-icon class="el-input__icon"><ele-Position /></el-icon>
					</template>
				</el-input>
			</el-col>
			<el-col :span="1"></el-col>
			<el-col :span="8">
				<el-button class="login-content-code" v-waves><el-image fit="fill" :src="imageCaptcha" alt="验证码"
						class="h-[38px]" @click="getCaptcha" /></el-button>
			</el-col>
		</el-form-item>
		<el-form-item class="login-animation4">
			<el-button type="primary" class="login-content-submit" round v-waves @click="onSignIn"
				:loading="loading.signIn">
				<span>登录</span>
			</el-button>
		</el-form-item>
	</el-form>
</template>

<script setup lang="ts" name="loginAccount">
import { reactive, ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '/@/stores/themeConfig';
import { initFrontEndControlRoutes } from '/@/router/frontEnd';
import { initBackEndControlRoutes } from '/@/router/backEnd';
import { NextLoading } from '/@/utils/loading';
import { useUserInfo } from '/@/stores/userInfo';
import { messageSuccess } from '/@/utils/message';
import { captchaCreate } from '/@/api/login';

// 定义变量内容
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
const route = useRoute();
const router = useRouter();
const loginFormRef = ref()

const loginForm = reactive({
	account: '',
	password: '',
	code: '',
	captchaUniqid: ''
})

const loginRules = reactive({
	account: [
		{ required: true, message: '账号不能为空', trigger: 'blur' }
	],
	password: [
		{ required: true, message: '密码不能为空', trigger: 'blur' }
	],
	code: [
		{ required: true, message: '验证码不能为空', trigger: 'blur' }
	]
})

const loading = reactive({
	signIn: false,
})

const imageCaptcha = ref('');

const getCaptcha = async () => {
	const res = await captchaCreate();
	imageCaptcha.value = res.image;
	loginForm.captchaUniqid = res.uniqid;
}

// 登录
const onSignIn = async () => {
	//验证器检验
	await loginFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return false;
		loading.signIn = true;
		try {
			await useUserInfo().login({
				account: loginForm.account,
				password: loginForm.password,
				channel: 4,
				code: loginForm.code,
				captchaUniqid: loginForm.captchaUniqid
			})
			if (!themeConfig.value.isRequestRoutes) {
				//前端控制路由
				await initFrontEndControlRoutes();
			} else {
				//后端控制路由
				await initBackEndControlRoutes();
			}
			signInSuccess();
		} catch (error) {
			loading.signIn = false;
		}
	})
};

// 登录成功后的跳转
const signInSuccess = () => {
	// 登录成功，跳到转首页
	// 如果是复制粘贴的路径，非首页/登录页，那么登录成功后重定向到对应的路径中
	if (route.query?.redirect) {
		router.push({
			path: <string>route.query?.redirect,
			query: Object.keys(<string>route.query?.params).length > 0 ? JSON.parse(<string>route.query?.params) : '',
		});
	} else {
		router.push('/');
	}
	// 登录成功提示
	// 关闭 loading
	loading.signIn = false;
	messageSuccess('登录成功');
	// 添加 loading，防止第一次进入界面时出现短暂空白
	NextLoading.start();
};

onMounted(() => {
	getCaptcha();
})

</script>

<style scoped lang="scss">
.login-content-form {
	margin-top: 20px;

	@for $i from 1 through 4 {
		.login-animation#{$i} {
			opacity: 0;
			animation-name: error-num;
			animation-duration: 0.5s;
			animation-fill-mode: forwards;
			animation-delay: calc($i/10) + s;
		}
	}

	.login-content-code {
		width: 100%;
		padding: 0;
		font-weight: bold;
		letter-spacing: 5px;
	}

	.login-content-submit {
		width: 100%;
		letter-spacing: 2px;
		font-weight: 300;
		margin-top: 15px;
	}
}
</style>
